<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\common\model\ConsultingServer;
use app\index\dao\ConsultingDao;
use think\facade\Cache;

/**
 * Class ConsultingServerCheckService
 * @package app\index\service
 */
class ConsultingServerCheckService
{
    /**
     * 查询
     * @param int $id
     */
    public function read($id)
    {
        $remind = 102;
        $server = null;
        $key = 'adnumber';
        $number = Cache::get($key);

        $consulting = Consulting::where('id', $id)->find();

        if (!$consulting) {
            return [
                'remind' => $remind,
                'server' => $server,
                'number' => $number,
            ];
        }

        if ($consulting->consultingType == '1') {
            return [
                'remind' => $remind,
                'server' => $server,
                'number' => $number,
            ];
        }

        // 最近X条消息内是否存在咨询服务消息
        $message = ConsultingMessage::where('consultingId', $id)
            ->where('messageType', ConsultingMessage::SERVER)
            ->order('id', 'DESC')
            ->limit(10)
            ->find();

        if ($message) {
            $serverId = $message->content->id ?? null;
        } else {
            $serverId = null;
        }

        if (is_id($serverId)) {
            $remind = '101';
            $server = ConsultingServer::where('id', $serverId)
                ->find();
        }

        return [
            'remind' => $remind,
            'server' => $server,
            'number' => $number,
        ];
    }


}
